package com.hn.zhijian.system.service.impl;

import com.hn.zhijian.system.entity.SysRoleResource;
import com.hn.zhijian.system.mapper.RoleResourceMapper;
import com.hn.zhijian.system.service.IRoleResourceService;
import com.hn.zhijian.base.service.impl.BaseService;
import com.hn.zhijian.utils.Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author carl chen
 * @since 2019-11-17
 */
@Service
public class RoleResourceServiceImpl extends BaseService<RoleResourceMapper, SysRoleResource> implements IRoleResourceService {
    @Transactional
//    @Log(module = "系统角色", description = "修改角色权限")
    public Integer changeRolePermission(Long roleId, List<Long> targetResources) {
        if (!Utils.isEmpty(roleId)) {

            super.remove(getQueryWrapper().eq("role_id", roleId));

            if (!Utils.isEmpty(targetResources)) {
                List<SysRoleResource> roleResources = new ArrayList<>();

                for (Long resourceId : targetResources) {
                    SysRoleResource roleResource = new SysRoleResource();
                    roleResource.setRoleId(roleId);
                    roleResource.setResourceId(resourceId);
                    roleResources.add(roleResource);
                }

                super.saveBatch(roleResources);
                return 1;
            }
        }
        return -1;
    }
}
